/**
 * 角色表格
 */
import React, { Component } from 'react';
import moment from 'moment';
import PropTypes from 'prop-types';
import { Link } from 'dva/router';
import { Table } from 'antd';
import getPagination from '../../components/pagination';
import { onSetItemDecorator } from '../../components/export';

export const columns = [
	{
		title: '产品名称',
		dataIndex: 'name',
		key: 'name',
	},
	{
		title: '产品品类',
		dataIndex: 'productCategoryName',
		key: 'productCategoryName'
	},
	{
		title: 'Product Key',
		dataIndex: 'gizwitsProductKey',
		key: 'gizwitsProductKey'
	},
	{
		title: '设备数量',
		dataIndex: 'deviceCount',
		key: 'deviceCount'
	},
	{
		title: '修改时间',
		dataIndex: 'utime',
		key: 'utime',
		render: time => moment(time).format('YYYY-MM-DD HH:mm:ss')
	},
	{
		title: '操作',
		key: 'opt'
	}
];

@onSetItemDecorator
class MyTable extends Component {
	constructor(props, context) {
		super(props, context);
		const { notShow = [] } = props;

		if (props.edit) this.columns = columns.map(c => c.key !== 'opt' ? c : { ...c, render: this.renderOpt });
		else this.columns = columns.filter(c => c.key !== 'opt');
		this.columns = this.columns.filter(c => !notShow.includes(c.key));
	}

	onChange = (selectedRowKeys) => {
		this.props.select(selectedRowKeys);   // KKK?
	};

	renderOpt = (text, record) => {
		return <a onClick={() => this.props.edit(record.id)}>详情</a>;
	}

	render() {
		const { list, selectKeys, pagination, edit } = this.props;
		const rowSelection = edit ? {
			onChange: this.onChange,
			selectedRowKeys: selectKeys
		} : null;
		return (
			<Table
				rowKey="id"
				dataSource={list}
				columns={this.getColumns(this.columns)}
				pagination={pagination}
				rowSelection={rowSelection}
			/>
		);
	}
}

MyTable.propTypes = {

};

export default MyTable;
